package com.scaffold.photo.action;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.photo.entity.Album;
import com.scaffold.photo.service.AlbumService;

/**
 * 相册操作xhtml
 * @author wangjg
 */
@Controller
public class AlbumAction extends BasicBizAction{
	@Autowired
	private AlbumService albumService;
	
	/**
	 * 相册列表
	 * @param keyword 查询关键字
	 */
	@RequestMapping("/album/list.xhtml")
	public String list(ModelMap model, String name, String keyword){
		Page page = this.getPage(20);
		List<Album> list = albumService.pageQuery(page, name, keyword);
		model.put("albumList", list);
		model.put("page", page);

		return "/album/list.vm";
	}
	
	/**
	 * 编辑相册
	 * @param id 编号
	 */
	@RequestMapping("/album/edit.xhtml")
	public String edit(ModelMap model, Long id){
		if(id!=null){
			Album album = this.albumService.getAlbum(id);
			this.checkOwner(album);
			model.put("album", album);	
			model.put("_update", "Y");
		}
		return "/album/edit.vm"; 
	}
	
	/**
	 * 保存相册
	 */
	@RequestMapping("/album/save.xhtml")
	public String save(String _update, Long id){
		if(!"Y".equals(_update)){
			Album album = new Album();
			this.bindParams(album);
			this.setOwner(album);
			this.albumService.save(album);
		}else{
			Album album = this.albumService.getAlbum(id);
			this.checkOwner(album);
			this.bindParams(album);
			this.albumService.update(album);
		}
		return "redirect:/album/list.xhtml";
	}
	
	/**
	 * 删除相册
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/album/delete.xhtml")
	public String delete(Long id){
		Album album = this.albumService.getAlbum(id);
		this.checkOwner(album);
		this.albumService.delete(album);
		return "redirect:/album/list.xhtml";
	}

}
